package com.ishansong.ticket.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by hongtaozhang on 9/22/15.
 */
public class DateUtil {


    public DateUtil() {
    }

    public static Date createDate(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }

    public static long getTimeInMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    public static Calendar createCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Date startOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date endOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static Date addHours(Date date, int numOfHours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(10, numOfHours);
        return calendar.getTime();
    }

    public static Date addMinutes(Date date, int numOfMinutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(12, numOfMinutes);
        return calendar.getTime();
    }

    public static Date addDays(Date date, int numdays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, numdays);
        return calendar.getTime();
    }

    public static Date addWeeks(Date date, int numWeeks) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(3, numWeeks);
        return calendar.getTime();
    }

    public static Date addMonths(Date date, int numMonths) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, numMonths);
        return calendar.getTime();
    }

    public static Date addYears(Date date, int numYears) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(1, numYears);
        return calendar.getTime();
    }

    public static String formatDateTime(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(date);
        }
    }

    public static Date evalDate(String text) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        try {
            return formatter.parse(text);
        } catch (ParseException var3) {
            return null;
        }
    }

    public static boolean isWorkTime() {
        boolean flag = false;
        Date date = new Date();
        if(date.after(evalDateTime(formatDate(date) + " 08:00:00")) && date.before(evalDateTime(formatDate(date) + " 20:00:00"))) {
            flag = true;
        }

        return flag;
    }

    public static boolean isWorkTime(String startWorktime, String endWorkTime) {
        boolean flag = false;
        Date date = new Date();
        if(date.after(evalDateTime(formatDate(date) + " " + startWorktime)) && date.before(evalDateTime(formatDate(date) + " " + endWorkTime))) {
            flag = true;
        }

        return flag;
    }

    public static Date evalDateTime(String text) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return formatter.parse(text);
        } catch (ParseException var3) {
            return null;
        }
    }

    public static String formatDateTimestamp(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(date);
        }
    }

    public static String formatDate(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.format(date);
        }
    }

    public static String formatDateExceptDays(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            return formatter.format(date);
        }
    }

    public static String formatTime(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
            return formatter.format(date);
        }
    }

    public static String formatDateTime(Calendar calendar) {
        return calendar == null?null:formatDateTime(calendar.getTime());
    }

    public static String formatDateTimeStamp(Date date) {
        if(date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
            return formatter.format(date);
        }
    }

    public static Date today() {
        return new Date();
    }

    public static Date tomorrow() {
        return addDays(new Date(), 1);
    }

    public static Date yesterday() {
        return addDays(new Date(), -1);
    }

    public static Calendar getTimeOnly(Calendar calendar) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(11, calendar.get(11));
        cal.set(12, calendar.get(12));
        cal.set(13, calendar.get(13));
        return cal;
    }

    public static Calendar getDateOnly(Calendar calendar) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(1, calendar.get(1));
        cal.set(6, calendar.get(6));
        return cal;
    }

    public static Calendar getDateTimeOnly(Calendar calendar) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(1, calendar.get(1));
        cal.set(6, calendar.get(6));
        cal.set(11, calendar.get(11));
        cal.set(12, calendar.get(12));
        return cal;
    }

    public static boolean compareDay(Calendar calendar1, Calendar calendar2) {
        return calendar1.get(1) != calendar2.get(1)?false:calendar1.get(6) == calendar2.get(6);
    }

    public static Date getPreviousMonthStartDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.get(2);
        cal.set(2, cal.get(2) - 1);
        cal.set(5, cal.getActualMinimum(5));
        return cal.getTime();
    }

    public static Date getCurrentMonthStartDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.get(2);
        cal.set(2, cal.get(2));
        cal.set(5, cal.getActualMinimum(5));
        return cal.getTime();
    }

    public static Date getCurrentMonthLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.get(2);
        cal.set(2, cal.get(2));
        cal.set(5, cal.getActualMaximum(5));
        return cal.getTime();
    }

    public static Date getPreviousMonthLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.get(2);
        cal.set(2, cal.get(2) - 1);
        cal.set(5, cal.getActualMaximum(5));
        return cal.getTime();
    }

    public static Date startOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        int min = calendar.getActualMinimum(7);
        int current = calendar.get(7);
        calendar.add(7, min - current);
        return calendar.getTime();
    }

    public static Date endOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        int current = calendar.get(7);
        calendar.add(7, 6 - current);
        return calendar.getTime();
    }

    public static boolean isDateSaturday(Date date) {
        boolean returnVal = false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(7);
        if(day == 7) {
            returnVal = true;
        } else {
            returnVal = false;
        }

        return returnVal;
    }

    public static long getCompareDate(String startDate, String endDate) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = formatter.parse(startDate);
        Date date2 = formatter.parse(endDate);
        long l = date2.getTime() - date1.getTime();
        long d = l / 86400000L;
        return d;
    }

    /**
     * 获取前一天的数据
     * @return
     */
    public static String getTime() {
        Calendar   calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,   -1);
        calendar.set(11, 23);
        calendar.set(12, 30);
        calendar.set(13, 00);
        return new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static void main(String[] args) {
        System.out.println(getTime());
    }
}
